// accounting/balance_sheet/balance_sheet.js
let listBehavior = require('../../behavior/list');
let nowYear = new Date().getFullYear(),
  nowMonth = new Date().getMonth() + 1;
nowMonth = nowMonth > 9 ? nowMonth : '0'+nowMonth;
let defPeriod = nowYear + '' + nowMonth;
Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    quickFlag: false,
    account_period: defPeriod,

    //搜索
    searchList: [
      {field_id: 'account_period',field_name: '会计期',dfltVal: defPeriod,type: 'reference1', optionList: [],optionId: 'account_period',optionName: 'account_period'},
    ],

    list: [],
    activeTable: 'be'
  },
  //显示搜索
  showQuick(){
    this.setData({
      quickFlag: !this.data.quickFlag,
    })
  },
  //切换导航栏
  toggleNav(e){
    let tid = e.currentTarget.dataset.tid;
    this.setData({activeTable: tid});
  },
  //搜索数据
  searchData({detail}){
    let account_period = '';
    wx.$each(detail,(i,item)=>{
      if (item.CompareWith === 'account_period') {
        account_period = item.CompareValue
      }
    });
    let obj = {account_period};
    if (!account_period) {
      obj.account_period = defPeriod
    }
    this.setData(obj);
    this.getFirstPage();
  },

  //获取数据
  async getDataList(){
    let {account_period} = this.data;
    wx.showLoading();
    await this.sumReport(account_period);
    wx.hideLoading();
    wx.$post(3,'CustomAccountContent/query_custom_account_content',{oby: "project_id", account_period: account_period, table_id: 1001}).then(res=>{
      if (res.errcode != 0)return;
      let data = res.data.row || [],
        list = [];
      this.setData({list: data,page:1,total:data.length,limit: data.length});
    })
  },

  sumReport(period){
    let url1 = 'Voucher/sum_all_voucher',
      url2 = 'CustomAccount/update_auto_value',
      post = {"account_period":period };
    return new Promise(resolve => {
      wx.$post(3,url1,post,false).then(res=>{
        if (res.errcode != 0){
          wx.$toast('统计凭证失败');
          return;
        }
        wx.$post(3,url2,post,false).then(res=>{
          if (res.errcode != 0){
            wx.$toast('统计报表失败');
            return;
          }
          resolve();
        })
      })
    })
  },


  //获取辅助核算数据
  getAssistData(){
    let {period} = this.data;
    wx.$post(3,'CustomAccountContent/query_custom_account_content',{oby: "project_id", account_period: period, table_id: "1001"}).then(res=>{
      if (res.errcode != 0)return;
      let data = res.data.row || [],
        list = [];
      this.setData({balanceData: data});
    })
  },

  //获取下拉数据
  getOptionsList (arg){
    let [key,v,url,post] = arg;
    return new Promise((resolve,reject) => {
      wx.$post(v, url, post).then(res=>{
        if (res.errcode != 0){
          reject();
          return;
        }
        let data = res.data.row || [],
          list = data;
        resolve({[key]: list});
      });
    });
  },
  ready(){

    let parmaArr = [
        ['account_period',3,'Account/query_all_account_period',{"limit":0,"page":1}],  //会计期
      ],
      pmArr = [];
    wx.$each(parmaArr,(i,item)=>{
      pmArr.push(this.getOptionsList(item));
    });
    Promise.all(pmArr.map(p => p.catch(err => ''))).then(res=>{
      let resObj = {};
      wx.$each(res,(i,item)=>{
        resObj = {...resObj,...item}
      });
      let searchList = this.data.searchList;
      wx.$each(searchList,(i,item)=>{
        if (resObj[item.field_id]){
          item.optionList = resObj[item.field_id];
        }
      });
      this.setData({searchList})
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();
    this.getDataList();
    this.ready();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
